#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>

static void pr_times(clock_t, struct tms *, struct tms *);
static void do_cmd(const char *);

int main(int argc, char const * argv[])
{
    int i;
    
    if (argc == 1)
    {
        printf("usage: %s <cmdline><cmdline>...\n", argv[0]);
        exit(0);
    }

    setbuf(stdout, NULL);
    for (i = 1; i < argc; i ++)
        do_cmd(argv[i]);

    return 0;
}

static void do_cmd(const char *cmd)
{
    struct tms tmsstart, tmsend;
    clock_t start, end;
    int status;

    printf("\nCommand: %s\n", cmd);

    start = times(&tmsstart);
    status = system(cmd);
    end = times(&tmsend);

    pr_times(end - start, &tmsstart, &tmsend);
}

static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend)
{
    static long clktck = 0;

    if (clktck == 0)
        printf("_SC_CLK_TCK is %ld\n", clktck = sysconf(_SC_CLK_TCK));

    printf("real:  %7.2f\n", real / (double)clktck);
    printf("user:  %7.2f\n", (tmsend->tms_utime - tmsstart->tms_utime) / (double)clktck);
    printf("sys :  %7.2f\n", (tmsend->tms_stime - tmsstart->tms_stime) / (double)clktck);
    printf("child user:  %7.2f\n", (tmsend->tms_cutime - tmsstart->tms_cutime) / (double)clktck);
    printf("child sys :  %7.2f\n", (tmsend->tms_cstime - tmsstart->tms_cstime) / (double)clktck);

}
